package org.hako.recite.word.parser.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import org.hako.recite.word.parser.WordList;
import org.hako.recite.word.parser.WordListParseException;
import org.hako.recite.word.parser.WordListParser;

/**
 * Abstract implement of {@link WordListParser}.
 * 
 * @author xnnyygn
 */
public abstract class AbstractWordListParser implements WordListParser {

    @Override
    public WordList parse(String filePath) throws WordListParseException {
        return parse(new File(filePath));
    }

    @Override
    public WordList parse(File file) throws WordListParseException {
        InputStream inputStream = null;
        try {
            inputStream = new FileInputStream(file);
            return parse(inputStream);
        } catch (FileNotFoundException e) {
            throw new WordListParseException(e);
        } catch (WordListParseException e) {
            throw e;
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    // omit
                }
            }
        }
    }
}
